<?php
	include '../init.php';
	$data = array(
		'title'=>addslashes($_POST['title']),
		'content'=>addslashes($_POST['content']),
		'keyword'=>addslashes($_POST['keyword']),
		'description'=>addslashes($_POST['description']),
		'author'=>addslashes($_POST['author']),
		'img'=>addslashes($_POST['img']),
		'status'=>(int)$_POST['status'],
		);
	
	if (isset($_POST['id'])&&(int)$_POST['id']>0) {
		$id=(int)$_POST['id'];
		$res = $mysql -> update('article',$data,"id={$id}");
		//下--更新--分类文章关联表
		//先删除原有的分类文章表的数据
		$sql = "DELETE FROM article_to_category WHERE article_id=$id";
		$mysql->query($sql);
		//然后添加细心地数据
		foreach ($_POST['cid'] as $key => $value) {
			$cateData[]=array('article_id'=>$id,'category_id'=>$value);
		}
		$mysql->addAll('article_to_category',$cateData);
		//上-分类文章关联部分
		if ($res>0) {
			echo json_encode(array('code'=>'000','msg'=>'保存成功'));
		}else{
			echo json_encode(array('code'=>'400','msg'=>'保存失败！'));
		}
	}else{
		$data['add_time']=time();
		$id = $mysql->addRow('article',$data);

		//下--添加--分类文章关联表用addRow函数
		// $data_id['article_id']=$id;
		// $cate_id=$_POST['cid'];
		// foreach ($cate_id as $key => $value) {
		// 	$data_id['category_id ']=$value;
		// 	$a_c=$mysql->addRow('article_to_category',$data_id);
		// }
		//下--添加--分类文章关联表用addAll函数
		foreach ($_POST['cid'] as $key => $value) {
			$cateData[]=array('article_id'=>$id,'category_id'=>$value);
		}
		$mysql->addAll('article_to_category',$cateData);
		//上-分类文章关联部分

		if ($id>0) {
			echo json_encode(array('code'=>'000','msg'=>'保存成功'));
		}else{
			echo json_encode(array('code'=>'400','msg'=>'保存失败！'));
		}
	
	}
	
?>